export class FPSMeter {
  private element: HTMLElement;
  private lastTime = performance.now();
  private frameCount = 0;
  private fps = 60;

  constructor(elementId = 'fps') {
    const el = document.getElementById(elementId);
    if (!el) throw new Error(`FPS计数器挂载元素 #${elementId} 未找到`);
    this.element = el;
  }

  tick() {
    const now = performance.now();
    this.frameCount++;
    
    if (now - this.lastTime >= 1000) {
      this.fps = this.frameCount;
      this.frameCount = 0;
      this.lastTime = now;
      this.element.textContent = `${this.fps} FPS`;
    }
  }

  getFPS(): number {
    return this.fps;
  }
}